package com.data.security.modelo;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class ControlPin {

	private EditText uno, dos, tres, cuatro;
	private String auxiliar[];
	private Activity contexto;

	public ControlPin(Activity contexto,EditText uno, EditText dos, EditText tres, EditText cuatro) {
	
		this.uno = uno;
		this.dos = dos;
		this.tres = tres;
		this.cuatro = cuatro;
		auxiliar = new String[4];
		this.contexto=contexto;
		cargarTextChanged();
	}


	public void limpiarCampos() {
		uno.setText("");
		dos.setText("");
		tres.setText("");
		cuatro.setText("");
	}

	private void cargarTextChanged() {
		
	    uno.addTextChangedListener(new TextWatcher() {
			
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					
					auxiliar [0]=s.toString();
				
					if (s.toString().length()>=1){
						contexto.runOnUiThread(new Runnable(){
							public void run(){
							
								dos.requestFocusFromTouch();
								
							}
							
						});
						
					}
					
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					
					
				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					
				}
				
			});
	    
	    dos.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				auxiliar [1]=s.toString();
				
				if (s.toString().length()>=1){
					contexto.runOnUiThread(new Runnable(){
						public void run(){
							uno.setText(auxiliar[0]);
							tres.requestFocusFromTouch();
							
						}
						
					});
					
				}
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	    
	     tres.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				auxiliar [2]=s.toString();
				
				if (s.toString().length()>=1){
					contexto.runOnUiThread(new Runnable(){
						public void run(){
							dos.setText(auxiliar[1]);
							cuatro.requestFocusFromTouch();
							
						}
						
					});
					
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	    
	    cuatro.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				auxiliar [3]=s.toString();
			
				if (s.toString().length()>=1){
					contexto.runOnUiThread(new Runnable(){
						public void run(){
							tres.setText(auxiliar[2]);
							
							
						}
						
					});
					
				}
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});

	}
	
	public String getText(){
		return (auxiliar[0]+auxiliar[1]+auxiliar[2]+auxiliar[3]);
	}

}
